package buzzTest;

import javax.sound.midi.*;
import javax.sound.midi.MidiDevice.Info;

import midi.SequencerSynchronizer;

public class BuzzTest {
	private static Sequencer sequencer;
	
	public static void main(String args[]){
		try{
			Info[] infos = MidiSystem.getMidiDeviceInfo();
			Info info = null;
			Info syncInfo = null;
			for (Info currInfo : infos)
			{
				if(currInfo.toString().contains("Out") && 
						currInfo.toString().contains("MIDI Yoke:") &&
						currInfo.toString().contains("1"))
				{
					info = currInfo;
					System.out.println("Selected: " + info.toString());
				}
				
				if(currInfo.toString().contains("Out") &&
						currInfo.toString().contains("MIDI Yoke:") &&
						currInfo.toString().contains("8"))
				{
					syncInfo = currInfo;
				}
				System.out.println(currInfo);
			}
			
			System.out.println("Info: " + info.toString());
			MidiDevice MY1device = MidiSystem.getMidiDevice(info);
			MidiDevice MY8device = MidiSystem.getMidiDevice(syncInfo);
			MY1device.open();
			MY8device.open();
			sequencer = MidiSystem.getSequencer(false);
			sequencer.getTransmitter().setReceiver(MY1device.getReceiver());
			sequencer.setTempoInBPM(126);
			sequencer.open();
			sequencer.setTempoFactor(1);
			
			Sequence sequence = new Sequence(Sequence.PPQ, 4);
			Track track = sequence.createTrack();
			
			ShortMessage msg = new ShortMessage();
			msg.setMessage(ShortMessage.CONTROL_CHANGE, 1, 1, 10);
			
			track.add(new MidiEvent(msg, 0));
			
			ShortMessage msg2 = new ShortMessage();
			msg2.setMessage(ShortMessage.CONTROL_CHANGE, 1, 1, 127);
			
			track.add(new MidiEvent(msg2, 32));
			track.add(new MidiEvent(msg2, 64));
			
			sequencer.setSequence(sequence);
			sequencer.setLoopStartPoint(0);
			sequencer.setLoopEndPoint(64);
			sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
			sequencer.setTickPosition(0);
			sequencer.start();
			
			
			
			SequencerSynchronizer sync = new SequencerSynchronizer(126, 1, MY8device.getReceiver(), Sequence.PPQ, 0, 1, 0);
			
			while (1 == 1)
				;
			
			
		} catch (Exception e){
			
		}
	}
}
